package com.homework.controller;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.homework.content.BaseContext;
import com.homework.dto.TeacherDto;
import com.homework.entity.Assignment;
import com.homework.entity.StudentAssignment;
import com.homework.entity.Teacher;
import com.homework.pojo.Result;
import com.homework.service.*;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("teacher")
public class TeacherController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private AssignmentService assignmentService;
    @Autowired
    private StudentAssignmentService studentAssignmentService;
    @Autowired
    private StudentTeacherService studentTeacherService;

    /**
     * 获取个人信息
     *
     * @return
     */
    @GetMapping("my")
    public Result my() {
        Long userId = BaseContext.getCurrentId();
        return Result.success(teacherService.lambdaQuery()
                .eq(Teacher::getTeacherId, userId)
                .list());
    }

    /**
     * 修改个人信息
     *
     * @param teacherDto
     * @return
     */
    @PutMapping("my")
    public Result update(@RequestBody @Valid TeacherDto teacherDto) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherDto, teacher);
        teacher.setTeacherId(BaseContext.getCurrentId().intValue());
        teacher.setPassword(MD5.create().digestHex16(teacher.getPassword()));
        teacherService.updateById(teacher);
        return Result.success("修改成功");
    }

    /**
     * 获取我的学生信息
     *
     * @return
     */
    @GetMapping("myStudents")
    public Result myStudents() {
        Long userId = BaseContext.getCurrentId();
        return Result.success(studentTeacherService.myStudents(userId));
    }

    /**
     * 我布置的作业
     *
     * @return
     */
    @GetMapping("myAssignments")
    public Result myAssignments() {
        Long userId = BaseContext.getCurrentId();
        return Result.success(assignmentService.lambdaQuery()
                .eq(Assignment::getAssignedBy, userId)
                .list());
    }

    /**
     * 上传作业
     * @param assignment
     * @return
     */
    @PostMapping("assignment")
    public Result assignment(@RequestBody Assignment assignment) {
        assignment.setAssignedBy(BaseContext.getCurrentId().intValue());
        assignment.setAssignedDate(new Date());
        assignmentService.save(assignment);
        return Result.success("发布成功");
    }

    /**
     * 学生作业完成情况
     * @param assignmentId 作业id
     * @return
     */
    @GetMapping("myStudentAssignments")
    public Result myStudentAssignments(Integer assignmentId) {
        Long userId = BaseContext.getCurrentId();
        return studentAssignmentService.myStudentAssignments(userId, assignmentId);
    }

    /**
     * 评价学作业
     * @param id 作业id
     * @param grade 评分 0~100之间
     * @param feedback 批注
     * @return
     */
    @PutMapping("/gradingAssignments/{id}")
    public Result gradingAssignments(@PathVariable Integer id, @RequestParam Integer studentId, @RequestParam BigDecimal grade, @RequestParam String feedback) {
        if (grade == null || grade.compareTo(BigDecimal.ZERO) < 0)
            return Result.error("评分不能小于0");
        if (grade.compareTo(new BigDecimal("100")) > 0)
            return Result.error("评分不能大于100");
        StudentAssignment studentAssignment = new StudentAssignment();
        studentAssignment.setGrade(grade);
        studentAssignment.setFeedback(feedback);
        studentAssignment.setAssignmentId(id);
        studentAssignment.setStudentId(studentId);
        studentAssignmentService.update(studentAssignment,
                new LambdaQueryWrapper<StudentAssignment>()
                .eq(StudentAssignment::getAssignmentId, id)
                .eq(StudentAssignment::getStudentId, studentId));
        return Result.success("评分成功");
    }
}
